package com.czl.module_service.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.RelativeLayout
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.SizeUtils
import com.czl.module_service.R
import com.czl.module_service.databinding.LayoutSuperRelativeBinding


/**
 * @author by xubin,
 * @date on 2022/1/19.
 * PS: Not easy to write code, please indicate.
 */
class SuperRelativeLayout(context: Context?, attrs: AttributeSet?) :
    RelativeLayout(context, attrs) {

    private var mLeftTextString: String? = null
    private var mLeftTextSize: Int
    private var mLeftTextColor: Int

    private var mRightTextString: String? = null
    private var mRightTextSize: Int
    private var mRightTextColor: Int

    private var mBottomLineColor: Int

    private var mDefaultDividerLineColor: Int = Color.parseColor("#FFE8E8E8")

    private var mBottomLineHeight: Float?

    private var binding: LayoutSuperRelativeBinding =
        LayoutSuperRelativeBinding.inflate(LayoutInflater.from(context), this, true)

    init {

        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.SuperRelativeLayout)
        typedArray.let {
            mLeftTextColor =
                it!!.getColor(R.styleable.SuperRelativeLayout_sLeftTextColor, Color.BLACK)
            mLeftTextString = it.getString(R.styleable.SuperRelativeLayout_sLeftTextString)
            mLeftTextSize = it.getDimensionPixelSize(
                R.styleable.SuperRelativeLayout_sLeftTextSize,
                SizeUtils.sp2px(12f)
            )

            mRightTextString = it.getString(R.styleable.SuperRelativeLayout_sRightTextString)
            mRightTextSize = it.getDimensionPixelSize(
                R.styleable.SuperRelativeLayout_sRightTextSize,
                SizeUtils.sp2px(12f)
            )
            mRightTextColor =
                it.getColor(R.styleable.SuperRelativeLayout_sLeftTextColor, Color.BLACK)

            mBottomLineColor = it.getColor(
                R.styleable.SuperRelativeLayout_sBottomLineColor,
                mDefaultDividerLineColor
            )

            mBottomLineHeight = it.getDimension(R.styleable.SuperRelativeLayout_sRightTextSize, 1f)
        }
        typedArray?.recycle()

        binding.tvLeft.text = mLeftTextString
        binding.tvLeft.run {
            text = mLeftTextString
            setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize.toFloat())
            setTextColor(mLeftTextColor)
        }

        binding.tvRight.run {
            text = mRightTextString
            setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize.toFloat())
            setTextColor(mRightTextColor)
        }

        binding.viewLine.run {
            setBackgroundColor(mBottomLineColor)
        }

        DataBindingUtil.bind<LayoutSuperRelativeBinding>(binding.root)
    }

    fun setRightText(text: String?) {
        binding.tvRight.text = text
    }
}